/**
 * 
 */
package hihocoder;

import java.util.Scanner;

/**
 * 字母表26
 * 
 *
 */
public class Trie树 {

	static class Node {
		Node[] childs = new Node[26];
		boolean isEnd = false;// 自身是不是终点
		int visitedCount = 1;

		// @Override
		// public String toString() {
		// String string = this.visitedCount + " \n";
		// for (int i = 0; i < childs.length; i++) {
		// if (childs[i] == null)
		// continue;
		// char ch = 'a';
		// ch += i;
		// string += " " + ch + "对应节点\n\t" + childs[i].toString();
		// }
		// return string;
		// }
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		scanner.nextLine();
		Node root = new Node();
		for (int i = 0; i < n; i++) {
			String string = scanner.nextLine();
			Node curNode = root;
			for (int j = 0; j < string.length(); j++) {
				int ch = string.charAt(j) - 'a';
				if (curNode.childs[ch] == null) {
					curNode.childs[ch] = new Node();
				} else {
					curNode.childs[ch].visitedCount++;
				}

				curNode = curNode.childs[ch];
				if (j == string.length() - 1) {
					curNode.isEnd = true;
				}
			}
		}
		// 询问
		int m = scanner.nextInt();
		scanner.nextLine();
		for (int i = 0; i < m; i++) {
			String string = scanner.nextLine();
			Node curNode = root;
			boolean notag = false;
			int len = string.length();
			for (int j = 0; j < len; j++) {
				int ch = string.charAt(j) - 'a';
				if (curNode.childs[ch] == null) {
					notag = true;
					break;
				}
				curNode = curNode.childs[ch];
			}
			// 最后一个节点的次数
			if (notag) {
				System.out.println(0);
			} else {
				System.out.println(curNode.visitedCount);
			}
		}
	}
}
